const sql_utils = load({
	id: 'sql_utils.js',
	appName: '公共脚本'
});
const base_service = load({
	id: 'base_service.js',
	appName: '公共脚本'
});

var loginDatas = JSON.stringify({
	"parameters": ["62011676a6107e", "Administrator", "DELL123.dell123",
		2052
	]
})
let Fid = 0
informat.plugin = {
	watch: [{
		module: "供应商管理",
		// 插入数据之后
		beforeInsertRecord: (ctx) => {

			// console.log('供应商管理', ctx);
			// console.log('ctx.tablefieldList------<', JSON.stringify(ctx.tableFieldList));
			const insertData = sql_utils.convertId2Alias(ctx.tableFieldList, ctx.insertData);
			console.log('insertData----->', insertData);
			//  console.log('ctx.insertData----->', insertData);
			//{id: undefined, FPayCurrencyId_name: "人民币", FPayCurrencyId: "i1cqjuk8x2v6", FName: "123", FGroup_name: "设备", FGroup: "i1cqjuk8x2v6", FCreateOrgId: 12, FUseOrgId: 121} 
			var data =
				//:\"FPayCurrencyId\",\"Message\":\"字段“结算币别”是必填项\",\"DIndex\":0},{\"FieldName\":\"FUseOrgId\",\"Message\":\"字段“使用组织”是必填项\",\"DIndex\":0},{\"FieldName\":\"FCreateOrgId\",\"Message\":\"字段“创建组织”是必填项\
				{
					"NeedUpDateFields": [],
					"NeedReturnFields": [],
					"IsDeleteEntry": "true",
					"SubSystemId": "",
					"IsVerifyBaseDataField": "false",
					"IsEntryBatchFill": "true",
					"ValidateFlag": "true",
					"NumberSearch": "true",
					"IsAutoAdjustField": "false",
					"InterationFlags": "",
					"IgnoreInterationFlag": "",
					"Model": {
						"FSupplierId": 0,
						"FCreateOrgId": {
							"FNumber": `${insertData.FCreateOrgId}`
						},
						"FUseOrgId": {
							"FNumber": `${insertData.FUseOrgId}`
						},
						"FName": insertData.FName,
						"FBaseInfo": {
							"FSupplyClassify": "CG"
						},
						"FBusinessInfo": {
							"FVmiBusiness": false,
							"FEnableSL": false
						},
						"FFinanceInfo": {
							"FPayCurrencyId": {
								"FNumber": "PRE001"
							},
							"FTaxType": {
								"FNumber": "SFL02_SYS"
							},
							"FInvoiceType": "1",
							"FTaxRateId": {
								"FNUMBER": "SL04_SYS"
							}
						}
					}
				}

			//请求k3的保存接口
			// console.log(JSON.stringify(data))
			//  这是一个request请求
			const loginResp = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
				},
				body: loginDatas
			});
			//JSON.parse() 方法是数据转换为 JS 对象
			console.log("loginResp")
			const loginData = JSON.parse(loginResp.content);
			const contentResp = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
					"kdservice-sessionid": loginData.KDSVCSessionId
				},
				//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
				body: JSON.stringify({
					"formid": "BD_Supplier",
					data
				})
			});
			console.log("contentResp---->", contentResp)
			const contentData = JSON.parse(contentResp.content);
			//获取到保存的Id
			var Id = contentData.Result.Id;
			Fid = Id
			console.log("Fid----》", Id)
			//
			var requestDate = {
				"CreateOrgId": 0,
				"Numbers": [],
				"Ids": `${Id}`,
				"SelectedPostId": 0,
				"NetworkCtrl": "",
				"IgnoreInterationFlag": ""
			}
			//console.log(requestDate)
			//请求k3的提交接口
			const rps = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
					"kdservice-sessionid": loginData.KDSVCSessionId
				},
				//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
				body: JSON.stringify({
					"formid": "BD_Supplier",
					"data": requestDate
				})
			});

			//console.log("contentData====>",contentData.Result.Id)

			// console.log("errors====>",contentData.Result.ResponseStatus.Errors)
			/**      if (resp.code !== 200) {
                    throw new Error('同步失败');
                }
*/
		},
		// 插入数据之后
		afterInsertRecord: (ctx) => {
			console.log('ctx---->', Object.keys(ctx));
			console.log(ctx.recordId)
			//修改添加我们对应的一个k3的那边的一个id
			//?key=0Zs2s2nNO17CCgo&request={"id":"0Zs2s2nPwT6UKX4","rowData":{"0Zs2s2nNE6bTA8w":"王五2"},"updateFields":["0Zs2s2nNE6bTA8w"]}
			var key = "0Z8f8fbPNLO3T9e"
			var DataId = ctx.recordId
			var table = informat.table.getTable('供应商管理');
			console.log(table)
			var fieldList = informat.table.getFieldList(table.id);
			console.log(Fid)
			var bean = {
				'k3Id': Fid
			}
			bean = informat.table.convert(fieldList, bean)
			informat.table.update(table.id, DataId, bean);
			//   console.log(resopen)
		},
		//更新数据之前
		afterUpdateRecord: (ctx) => {
			console.log('afterupdateRecord----《》', Object.keys(ctx));
			// console.log(sql_utils.convertId2Alias(ctx.tableFieldList, ctx.updateData))
			var insertData = sql_utils.convertId2Alias(ctx.tableFieldList, ctx.newRecord)
			var k3Id = insertData.K3Id
			var data =
				//:\"FPayCurrencyId\",\"Message\":\"字段“结算币别”是必填项\",\"DIndex\":0},{\"FieldName\":\"FUseOrgId\",\"Message\":\"字段“使用组织”是必填项\",\"DIndex\":0},{\"FieldName\":\"FCreateOrgId\",\"Message\":\"字段“创建组织”是必填项\
				{
					"NeedUpDateFields": [],
					"NeedReturnFields": [],
					"IsDeleteEntry": "true",
					"SubSystemId": "",
					"IsVerifyBaseDataField": "false",
					"IsEntryBatchFill": "true",
					"ValidateFlag": "true",
					"NumberSearch": "true",
					"IsAutoAdjustField": "false",
					"InterationFlags": "",
					"IgnoreInterationFlag": "",
					"Model": {
						"FSupplierId": k3Id,
						"FCreateOrgId": {
							"FNumber": `${insertData.FCreateOrgId}`
						},
						"FUseOrgId": {
							"FNumber": `${insertData.FUseOrgId}`
						},
						"FName": insertData.FName,
					}
				}


			var fh = {
				"CreateOrgId": 0,
				"Numbers": [],
				"Ids": `${k3Id}`,
				"InterationFlags": "",
				"IgnoreInterationFlag": "",
				"NetworkCtrl": "",
				"IsVerifyProcInst": ""
			}
			const loginResp = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
				},
				body: loginDatas
			});
			//JSON.parse() 方法是数据转换为 JS 对象
			console.log("loginResp")
			const loginData = JSON.parse(loginResp.content);

			//反审核
			const contentResp = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
					"kdservice-sessionid": loginData.KDSVCSessionId
				},
				//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
				body: JSON.stringify({
					"formid": "BD_Supplier",
					"data": fh
				})
			});
			// console.log("contentResp---->",contentResp)

			//k3 接口暂存


			const zhancui = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Draft.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
					"kdservice-sessionid": loginData.KDSVCSessionId
				},
				//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
				body: JSON.stringify({
					"formid": "BD_Supplier",
					"data": data
				})
			});

			console.log('zhancui----->', zhancui);

			var requestDate = {
				"CreateOrgId": 0,
				"Numbers": "",
				"Ids": `${k3Id}`,
				"SelectedPostId": 0,
				"NetworkCtrl": "",
				"IgnoreInterationFlag": ""
			}
			//k3 的提交
			//请求k3的提交接口
			const rps = informat.http.request({
				method: "POST",
				url: "http://shineholder.wicp.net/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc",
				timeout: 30000,
				header: {
					"Content-Type": "application/json",
					"kdservice-sessionid": loginData.KDSVCSessionId
				},
				//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
				body: JSON.stringify({
					"formid": "BD_Supplier",
					"data": requestDate
				})
			});
			console.log(rps)
		}
	}]
}
